﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Linq;
#endregion

namespace WinnerCMS.BLL.PageFactory.HtmlFactory
{
    public class UrlPager
    {
        #region *** enum ***

        /// <summary>
        /// 自定义文本显示的位置
        /// </summary>
        public enum TextAlign
        {
            /// <summary>
            /// 左边
            /// </summary>
            Left = 0,

            /// <summary>
            /// 右边
            /// </summary>
            Right = 1
        }

        #endregion

        #region *** varables,constants,attributes ***

        private readonly StringBuilder _sbMainHtml = new StringBuilder();

        /// <summary>
        /// 开始数字页码
        /// </summary>
        private int _startIndex;

        /// <summary>
        /// 结束数字页码
        /// </summary>
        private int _endIndex;

        /// <summary>
        /// 可用的最大数字页码个数
        /// </summary>
        private int _maxNumBtnCount;

        public string CssClass { get; set; }

        private bool _alwayShow = true;

        /// <summary>
        /// 当记录总数为0时是否总是显示分页导航
        /// </summary>
        public bool AlwayShow
        {
            get { return this._alwayShow; }
            set { this._alwayShow = value; }
        }

        /// <summary>
        /// 当上一页/下一页/首页/尾页无效时是否总是显示他们
        /// </summary>
        public bool AlwayShowPreviousNextFirstLast { get; set; }

        private string _gotoBoxTextBoxCssClass = "txt";

        /// <summary>
        /// [跳转到第N页]快捷工具文本框的样式类名
        /// </summary>
        public string GotoBoxTextBoxCssClass
        {
            get { return this._gotoBoxTextBoxCssClass; }
            set { this._gotoBoxTextBoxCssClass = value; }
        }

        private string _gotoBoxButtonCssClass = "btn";

        /// <summary>
        /// [跳转到第N页]快捷工具按钮的样式类名
        /// </summary>
        public string GotoBoxButtonCssClass
        {
            get { return this._gotoBoxButtonCssClass; }
            set { this._gotoBoxButtonCssClass = value; }
        }

        /// <summary>
        /// [页码下拉列表]的样式类名
        /// </summary>
        public string PageListCssClass { get; set; }

        private string _tagName = "div";

        /// <summary>
        /// 控件标签名称
        /// </summary>
        public string TagName
        {
            get { return this._tagName; }
            set { this._tagName = value; }
        }

        private string _pageUrlParameter = "PageNo";

        /// <summary>
        /// 获取或设置页码URL参数名称
        /// </summary>
        public string PageUrlParameter
        {
            get { return this._pageUrlParameter; }
            set { this._pageUrlParameter = value; }
        }

        private int _pageSize = 20;

        /// <summary>
        /// 页大小（每页显示的最大记录数）
        /// </summary>
        public int PageSize
        {
            get { return this._pageSize; }
            set
            {
                if (value < 1)
                {
                    throw new ArgumentException("参数 PageSize（页大小）必须为正整数。");
                }

                this._pageSize = value;
            }
        }

        private int? _pageIndex;

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageNo
        {
            get
            {
                if (this._pageIndex == null)
                {
                    string tmpS = HttpContext.Current.Request.QueryString[PageUrlParameter];

                    if (string.IsNullOrEmpty(tmpS))
                    {
                        this._pageIndex = 1;
                    }
                    else
                    {
                        int tmpI;

                        if (int.TryParse(tmpS, out tmpI))
                        {
                            this._pageIndex = tmpI;
                        }
                        else
                        {
                            this._pageIndex = 1;
                        }
                    }

                    //如果请求的页码大于最大页码，则返回当前最大页码
                    if (PageNo > PageCount)
                    {
                        PageNo = PageCount;
                    }
                }

                return this._pageIndex.Value;
            }
            set
            {
                if (value < 1)
                {
                    throw new ArgumentException("参数 PageIndex（当前页码）必须为正整数。");
                }

                this._pageIndex = value;
            }
        }

        private int _recordCount;

        /// <summary>
        /// 记录总数
        /// </summary>
        public int RecordCount
        {
            get { return this._recordCount; }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("参数 RecordCount（记录总数）必须为非负整数。");
                }

                this._recordCount = value;
            }
        }

        private int _pageCount = -1;

        /// <summary>
        /// 获取页码总数
        /// </summary>
        public int PageCount
        {
            get
            {
                if (this._pageCount < 0)
                {
                    this._pageCount =
                        Convert.ToInt32(Math.Ceiling(Convert.ToDouble(RecordCount) / Convert.ToDouble(PageSize)));
                }

                return this._pageCount;
            }
        }

        private string _previousPageText = "&lsaquo;&lsaquo;";

        /// <summary>
        /// [上一页]呈现的内容
        /// </summary>
        public string PreviousPageText
        {
            get { return this._previousPageText; }
            set { this._previousPageText = value; }
        }

        private bool _showPreviousPage = true;

        /// <summary>
        /// 是否显示[上一页]按钮
        /// </summary>
        public bool ShowPreviousPage
        {
            get { return this._showPreviousPage; }
            set { this._showPreviousPage = value; }
        }

        private string _nextPageText = "&rsaquo;&rsaquo;";

        /// <summary>
        /// [下一页]呈现的内容
        /// </summary>
        public string NextPageText
        {
            get { return this._nextPageText; }
            set { this._nextPageText = value; }
        }

        private bool _showNextPage = true;

        /// <summary>
        /// 是否显示[下一页]按钮
        /// </summary>
        public bool ShowNextPage
        {
            get { return this._showNextPage; }
            set { this._showNextPage = value; }
        }

        private string _firstPageText = "|&lsaquo;";

        /// <summary>
        /// [首页]呈现的内容
        /// </summary>
        public string FirstPageText
        {
            get { return this._firstPageText; }
            set { this._firstPageText = value; }
        }

        private string _gotoButtonText = "跳转";

        /// <summary>
        /// [跳转到第N页]按钮显示的文本
        /// </summary>
        public string GotoButtonText
        {
            get { return this._gotoButtonText; }
            set { this._gotoButtonText = value; }
        }

        private bool _showFirstPage = true;

        /// <summary>
        /// 是否显示[首页]按钮
        /// </summary>
        public bool ShowFirstPage
        {
            get { return this._showFirstPage; }
            set { this._showFirstPage = value; }
        }

        private string _lastPageText = "&rsaquo;|";

        /// <summary>
        /// [尾页]呈现的内容
        /// </summary>
        public string LastPageText
        {
            get { return this._lastPageText; }
            set { this._lastPageText = value; }
        }

        private bool _showLastPage = true;

        /// <summary>
        /// 是否显示[尾页]按钮
        /// </summary>
        public bool ShowLastPage
        {
            get { return this._showLastPage; }
            set { this._showLastPage = value; }
        }

        private string _firstPageCssClass = "fp";

        /// <summary>
        /// [首页]样式类名
        /// </summary>
        public string FirstPageCssClass
        {
            get { return this._firstPageCssClass; }
            set { this._firstPageCssClass = value; }
        }

        private string _previousPageCssClass = "pp";

        /// <summary>
        /// [上一页]样式类名
        /// </summary>
        public string PreviousPageCssClass
        {
            get { return this._previousPageCssClass; }
            set { this._previousPageCssClass = value; }
        }

        private string _nextPageCssClass = "np";

        /// <summary>
        /// [下一页]样式类名
        /// </summary>
        public string NextPageCssClass
        {
            get { return this._nextPageCssClass; }
            set { this._nextPageCssClass = value; }
        }

        private string _lastPageCssClass = "lp";

        /// <summary>
        /// [尾页]样式类名
        /// </summary>
        public string LastPageCssClass
        {
            get { return this._lastPageCssClass; }
            set { this._lastPageCssClass = value; }
        }

        private int _numericButtonCount = 10;

        /// <summary>
        /// 显示数字页码按钮的个数
        /// </summary>
        public int NumericButtonCount
        {
            get { return this._numericButtonCount; }
            set { this._numericButtonCount = value; }
        }

        private string _numericButtonTextFormatString = "<a href=\"{0}\">{1}</a>";

        /// <summary>
        /// 数字页码呈现的内容的格式。{0}表示超链接地址，{1}表示页码数字
        /// </summary>
        public string NumericButtonTextFormatString
        {
            get { return this._numericButtonTextFormatString; }
            set { this._numericButtonTextFormatString = value; }
        }

        private string _currentNumericButtonCssClass = "cur";

        /// <summary>
        /// 当前页数字按钮的样式类名
        /// </summary>
        public string CurrentNumericButtonCssClass
        {
            get { return this._currentNumericButtonCssClass; }
            set { this._currentNumericButtonCssClass = value; }
        }

        private bool _showNumericButton = true;

        /// <summary>
        /// 是否显示页码按钮
        /// </summary>
        public bool ShowNumericButton
        {
            get { return this._showNumericButton; }
            set { this._showNumericButton = value; }
        }

        private string _morePreviousNumericButtonText = "&lt;...";

        /// <summary>
        /// [前面还有更多页码]呈现的文本
        /// </summary>
        public string MorePreviousNumericButtonText
        {
            get { return this._morePreviousNumericButtonText; }
            set { this._morePreviousNumericButtonText = value; }
        }

        private string _moreNextNumericButtonText = "...&gt;";

        /// <summary>
        /// [后面还有更多页码]呈现的文本
        /// </summary>
        public string MoreNextNumericButtonText
        {
            get { return this._moreNextNumericButtonText; }
            set { this._moreNextNumericButtonText = value; }
        }

        private bool _showMoreNumericButton = true;

        /// <summary>
        /// 是否显示[更多页码按钮]
        /// </summary>
        public bool ShowMoreNumericButton
        {
            get { return this._showMoreNumericButton; }
            set { this._showMoreNumericButton = value; }
        }


        /// <summary>
        /// 分页控件主要部分容器标签名称
        /// </summary>
        public string PagerTagName { get; set; }

        private string _pagerCssClass = "main";

        /// <summary>
        /// 分页控件主要部分样式类名
        /// </summary>
        public string PagerCssClass
        {
            get { return this._pagerCssClass; }
            set { this._pagerCssClass = value; }
        }

        private string _customizeText = string.Empty;

        /// <summary>
        /// 自定义文本
        /// </summary>
        public string CustomizeText
        {
            get { return this._customizeText; }
            set { this._customizeText = value; }
        }

        private bool _showCustomizeText = true;

        /// <summary>
        /// 是否显示自定义文本
        /// </summary>
        public bool ShowCustomizeText
        {
            get { return this._showCustomizeText; }
            set { this._showCustomizeText = value; }
        }

        private TextAlign _customizeTextAlign = TextAlign.Left;

        /// <summary>
        /// 自定义文本呈现的位置
        /// </summary>
        public TextAlign CustomizeTextAlign
        {
            get { return this._customizeTextAlign; }
            set { this._customizeTextAlign = value; }
        }

        private static readonly Regex UrlRewriteRegex = new Regex(@"(.*?)(\d+)(\.[htm|html|aspx])",
            RegexOptions.IgnoreCase | RegexOptions.Compiled);

        private static readonly Regex MvcRegex = new Regex(@"(.*?)/(\d+)(.*?)",
            RegexOptions.IgnoreCase | RegexOptions.Compiled);

        private string _urlRewritePattern = string.Empty;

        /// <summary>
        /// URL重写格式。{0}表示页码
        /// </summary>
        public string UrlRewritePattern
        {
            get
            {
                if (string.IsNullOrEmpty(this._urlRewritePattern) == false)
                    return this._urlRewritePattern;
                string rawUrl = HttpContext.Current.Request.RawUrl;

                //return UrlRewriteRegex.Replace(rawUrl, "$1{0}$3");
                return MvcRegex.Matches(rawUrl)[0] + "/{0}";
            }
            set { this._urlRewritePattern = value; }
        }

        /// <summary>
        /// 是否启用URL重写
        /// </summary>
        public bool EnableUrlRewrite { get; set; }

        private string _urlPathFilename;

        /// <summary>
        /// URL请求的文件名
        /// </summary>
        public string UrlPathFilename
        {
            get
            {
                if (this._urlPathFilename == null)
                {
                    string absolutePath = HttpContext.Current.Request.Url.AbsolutePath;

                    int pos = absolutePath.LastIndexOf('/');

                    if (pos == -1)
                    {
                        this._urlPathFilename = absolutePath;
                    }
                    else
                    {
                        this._urlPathFilename = absolutePath.Substring(pos + 1);
                    }

                    return this._urlPathFilename;
                }

                return this._urlPathFilename;
            }
            set { this._urlPathFilename = value; }
        }

        private string _queryFormatString;

        /// <summary>
        /// 获取URL参数格式字符串
        /// </summary>
        private string QueryFormatString
        {
            get
            {
                if (this._queryFormatString != null)
                {
                    return this._queryFormatString;
                }

                if (IsMvc)
                {
                    string rawUrl = HttpContext.Current.Request.RawUrl;
                     
                    return MvcRegex.Matches(rawUrl)[0] + "/{0}";
                }

                //1 如果启用了URL重写，则返回URL重写规则
                if (EnableUrlRewrite)
                {
                    this._queryFormatString = UrlRewritePattern;
                    return this._queryFormatString;
                }

                //2 否则从URL参数里解析并重新整理
                string tmpS = HttpContext.Current.Request.Url.Query;

                //处理为页码格式化字符串
                //1 如果URL有page参数
                if (HttpContext.Current.Request.QueryString[PageUrlParameter] != null)
                {
                    tmpS = Regex.Replace(tmpS, PageUrlParameter + "=\\d*", PageUrlParameter + "={0}");
                }
                //2 如果URL没有page参数
                else
                {
                    //1 如果URL有其它参数
                    if (tmpS.IndexOf('?') != -1)
                    {
                        tmpS += "&";
                    }

                    tmpS += PageUrlParameter + "={0}";
                }

                //删除第一个问号
                tmpS = Regex.Replace(tmpS, @"^\?", string.Empty);

                this._queryFormatString = string.Format("{0}?{1}", UrlPathFilename, tmpS);

                return this._queryFormatString;
            }
            set { this._pageUrlParameter = value; }
        }

        /// <summary>
        /// 自定义布局格式化字符串。
        /// {$text$} 自定义文本，
        /// {$first$} 首页，
        /// {$pre$} 上一页，
        /// {$num$} 数字页码，
        /// {$next$} 下一页，
        /// {$last$} 尾页，
        /// {$pre_more$} 前面还有更多页码，
        /// {$next_more$} 后面还有更多页码，
        /// {$list$} 页码下拉列表，
        /// {$goto$} 直接跳转到第N页快捷工具
        /// </summary>
        public string CustomizeLayoutFormatString { get; set; }

        /// <summary>
        /// 是否启用自定义布局
        /// </summary>
        public bool EnableCustomizeLayout { get; set; }

        private string _gotoAlertFormatString = "页码必须是数字且不能大于最大页码 {$pc$}。";

        public UrlPager()
        {
            EnableCustomizeLayout = false;
            CustomizeLayoutFormatString = null;
            IsMvc = true;
            EnableUrlRewrite = true;
            PagerTagName = null;
            PageListCssClass = null;
            AlwayShowPreviousNextFirstLast = false;
        }

        /// <summary>
        /// 获取或设置[页码跳转]时输入的页码错误提示语，包含格式：{$pc$} 最大页码数
        /// </summary>
        public string GotoAlertFormatString
        {
            get { return this._gotoAlertFormatString; }
            set { this._gotoAlertFormatString = value; }
        }

        #endregion

        #region *** private methods ***

        /// <summary>
        /// 获取页码链接
        /// </summary>
        /// <param name="targetIndex">目标页码</param>
        /// <returns></returns>
        private string GetPageLink(int targetIndex)
        {
            return string.Format(QueryFormatString, targetIndex);
        }


        /// <summary>
        /// 获取相对页码数
        /// </summary>
        /// <param name="relativePage"></param>
        /// <returns></returns>
        private int GetRelativePageIndex(int relativePage)
        {
            return (PageNo + relativePage);
        }

        /// <summary>
        /// 获取[前面还有更多页码]HTML
        /// </summary>
        /// <returns></returns>
        private string GetMorePreviousNumericButton()
        {
            return string.Format(NumericButtonTextFormatString,
                string.Format(QueryFormatString, this._startIndex - 1), MorePreviousNumericButtonText);
        }

        /// <summary>
        /// 获取[后面还有更多页码]HTML
        /// </summary>
        /// <returns></returns>
        private string GetMoreNextNumericButton()
        {
            return string.Format(NumericButtonTextFormatString,
                string.Format(QueryFormatString, this._endIndex + 1), MoreNextNumericButtonText);
        }

        /// <summary>
        /// 获取数字页码按钮HTML代码
        /// </summary>
        /// <returns></returns>
        private string GetNumericButtonHtml()
        {
            /******************************
             * MNC = maxNumBtnCount
             * PC = PageCount
             * P = PageIndex
             * MPP = midPagePos
             * LNC = leftNumCount
             * RNC = rightNumCount
             ******************************/
            StringBuilder sbRtn = new StringBuilder();

            //如果开始页码不是1，则前面还页码
            if (this._startIndex != 1 && ShowMoreNumericButton && !EnableCustomizeLayout)
            {
                sbRtn.AppendFormat(GetMorePreviousNumericButton());
            }

            for (int i = this._startIndex; i <= this._endIndex; i++)
            {
                string tmpS;

                if (i == PageNo)
                {
                    tmpS = string.Format(NumericButtonTextFormatString,
                        ("javascript:void(0)") + string.Format("\" class=\"{0}", CurrentNumericButtonCssClass),
                        i);
                }
                else
                {
                    tmpS = string.Format(NumericButtonTextFormatString, string.Format(QueryFormatString, i), i);
                }

                sbRtn.Append(tmpS);
            } // end for

            //如果结束页码不是最大页码，则后面还有页码
            if (this._endIndex != PageCount && ShowMoreNumericButton && !EnableCustomizeLayout)
            {
                sbRtn.AppendFormat(GetMoreNextNumericButton());
            }

            return sbRtn.ToString();
        }

        /// <summary>
        /// 获取[首页]HTML
        /// </summary>
        /// <returns></returns>
        private string GetFirstPageHtml()
        {
            string valRtn;

            if (PageNo <= 1)
            {
                valRtn = string.Format("<a{0} href=\"javascript:void(0)\">{1}</a>",
                    FirstPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", FirstPageCssClass) : string.Empty,
                    FirstPageText);
            }
            else
            {
                valRtn = string.Format("<a{0} href=\"{1}\">{2}</a>",
                    FirstPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", FirstPageCssClass) : string.Empty,
                    GetPageLink(1), FirstPageText);
            }

            return valRtn;
        }

        /// <summary>
        /// 获取[上一页]HTML
        /// </summary>
        /// <returns></returns>
        private string GetPreviousPageHtml()
        {
            string valRtn;
            int targetIndex = GetRelativePageIndex(-1);

            if (targetIndex > 0)
            {
                valRtn = string.Format("<a{0} href=\"{1}\">{2}</a>",
                    PreviousPageCssClass.Length > 0
                        ? string.Format(" class=\"{0}\"", PreviousPageCssClass)
                        : string.Empty,
                    GetPageLink(targetIndex), PreviousPageText);
            }
            else
            {
                valRtn = string.Format("<a{0} href=\"javascript:void(0)\">{1}</a>",
                    PreviousPageCssClass.Length > 0
                        ? string.Format(" class=\"{0}\"", PreviousPageCssClass)
                        : string.Empty,
                    PreviousPageText);
            }

            return valRtn;
        }

        /// <summary>
        /// 获取[下一页]HTML
        /// </summary>
        /// <returns></returns>
        private string GetNextPageHtml()
        {
            string valRtn;

            int targetIndex = GetRelativePageIndex(1);

            if (targetIndex <= PageCount)
            {
                valRtn = string.Format("<a{0} href=\"{1}\">{2}</a>",
                    NextPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", NextPageCssClass) : string.Empty,
                    GetPageLink(targetIndex), NextPageText);
            }
            else
            {
                valRtn = string.Format("<a{0} href=\"javascript:void(0)\">{1}</a>",
                    NextPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", NextPageCssClass) : string.Empty,
                    NextPageText);
            }

            return valRtn;
        }

        /// <summary>
        /// 获取[尾页]HTML
        /// </summary>
        /// <returns></returns>
        private string GetLastPageHtml()
        {
            string valRtn;

            if (PageNo >= PageCount)
            {
                valRtn = string.Format("<a{0} href=\"javascript:void(0)\">{1}</a>",
                    LastPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", LastPageCssClass) : string.Empty,
                    LastPageText);
            }
            else
            {
                valRtn = string.Format("<a{0} href=\"{1}\">{2}</a>",
                    LastPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", LastPageCssClass) : string.Empty,
                    GetPageLink(PageCount), LastPageText);
            }

            return valRtn;
        }


        /// <summary>
        /// 初始化对象、参数、变量
        /// </summary>
        private void InitObjects()
        {
            this._maxNumBtnCount = Math.Min(NumericButtonCount, PageCount);

            //1 如果MNC = PC
            if (this._maxNumBtnCount == PageCount)
            {
                this._startIndex = 1;
                this._endIndex = PageCount;
            }
            //2 MNC > PC
            else
            {
                //第中间个页码位置
                int midPagePos;
                //左边页码个数
                int leftNumCount;
                //右边页码个数
                int rightNumCount;

                // 11/2 = 5
                midPagePos = (this._maxNumBtnCount / 2);

                //tmpI = ;
                //如果MNC为偶数
                if ((this._maxNumBtnCount % 2) == 0)
                {
                    leftNumCount = midPagePos - 1;
                    rightNumCount = midPagePos;
                }
                //如果MNC为奇数
                else
                {
                    leftNumCount = midPagePos;
                    rightNumCount = midPagePos;
                }

                this._startIndex = (PageNo - leftNumCount);
                this._endIndex = (PageNo + rightNumCount);


                while (this._startIndex < 1 || this._endIndex > PageCount)
                {
                    if (this._startIndex < 1)
                    {
                        rightNumCount += (Math.Abs(this._startIndex) + 1);
                        leftNumCount -= (Math.Abs(this._startIndex) + 1);
                    }
                    else if (this._endIndex > PageCount)
                    {
                        leftNumCount += (this._endIndex - PageCount);
                        rightNumCount -= (this._endIndex - PageCount);
                    }

                    this._startIndex = (PageNo - leftNumCount);
                    this._endIndex = (PageNo + rightNumCount);
                } // end while
            } // end if
        }

        /// <summary>
        /// 处理分页控件主要部分(默认布局)
        /// </summary>
        private string ProcessPagerMain()
        {
            if (!string.IsNullOrEmpty(TagName))
            {
                this._sbMainHtml.Append("<");
                this._sbMainHtml.Append(TagName);
                if (!string.IsNullOrEmpty(CssClass))
                {
                    this._sbMainHtml.Append(" class=\"" + CssClass + "\"");
                }
                this._sbMainHtml.Append(">");
            }

            //自定义文本
            if (ShowCustomizeText && CustomizeTextAlign == TextAlign.Left)
            {
                this._sbMainHtml.Append(CustomizeText);
            }

            //处理分页控件主要部分
            //如果有设置分页控件主要部分的窗口标签名称，则添加容器
            if (!string.IsNullOrEmpty(PagerTagName))
            {
                this._sbMainHtml.Append("<")
                    .Append(PagerTagName);

                if (!string.IsNullOrEmpty(PagerCssClass))
                {
                    this._sbMainHtml.AppendFormat(" class=\"{0}\"", PagerCssClass);
                }

                this._sbMainHtml.Append(">");
            }

            //添加[首页]按钮
            if (ShowFirstPage && (AlwayShowPreviousNextFirstLast || (PageNo != 1 && PageCount > 0)))
            {
                this._sbMainHtml.Append(GetFirstPageHtml());
            }

            //添加[上一页]按钮
            if (ShowPreviousPage && (AlwayShowPreviousNextFirstLast || (PageNo != 1 && PageCount > 0)))
            {
                this._sbMainHtml.Append(GetPreviousPageHtml());
            }


            //添加数字页码
            if (ShowNumericButton)
            {
                this._sbMainHtml.Append(GetNumericButtonHtml());
            }


            //添加[下一页]按钮
            if (ShowNextPage && (AlwayShowPreviousNextFirstLast || (PageNo != PageCount && PageCount > 0)))
            {
                this._sbMainHtml.Append(GetNextPageHtml());
            }

            //添加[尾页]按钮
            if (ShowLastPage && (AlwayShowPreviousNextFirstLast || (PageNo != PageCount && PageCount > 0)))
            {
                this._sbMainHtml.Append(GetLastPageHtml());
            }


            //如果有设置分页控件主要部分的窗口标签名称，则添加容器
            if (!string.IsNullOrEmpty(PagerTagName))
            {
                this._sbMainHtml.AppendFormat("</{0}>", PagerTagName);
            }


            //自定义文本
            if (ShowCustomizeText && CustomizeTextAlign == TextAlign.Right)
            {
                this._sbMainHtml.Append(CustomizeText);
            }


            //容器结束
            if (!string.IsNullOrEmpty(TagName))
            {
                this._sbMainHtml.Append("</");
                this._sbMainHtml.Append(TagName);
                this._sbMainHtml.Append(">");
            }

            return this._sbMainHtml.ToString();
        }

        #endregion

        /// <summary>
        /// 渲染控件
        /// </summary>
        public override string ToString()
        {
            if ((PageCount > 0) || (PageCount < 1 && AlwayShow))
            {
                InitObjects();

                return ProcessPagerMain();
            }

            return string.Empty;
        }

        public bool IsMvc { get; set; }
    }
}